<template>
  <InlineLayout title-name="单位信息列表">
    <template #header>
      <my-form ref="queryFormRef" v-model:modelValue="queryParams" :model="queryParams" :items-datas="itemsDatas">
        <my-button type="reset" title="重置" @click="resetQuery" />
        <my-button type="search" title="查询" :loading="loading" @click="handleQuery" />
      </my-form>
    </template>

    <template #titleTools>
      <my-button v-hasPermi="['yuanshidian:ab01:add']" type="add" title="单位新增" @click="handleAdd" />
      <my-button v-hasPermi="['yuanshidian:ab01:export']" type="export" title="单位导出" @click="handleExport" />
    </template>

    <my-table :columns="columns" :table-data="tableData" @selection-change="handleSelectionChange">
      <template #status="scope">
        <span v-if="scope.row.status === 1">待提交</span>
        <span v-if="scope.row.status === 2" style="color: #e6a23c">审核中</span>
        <span v-if="scope.row.status === 3" style="color: #67c23a">已审核</span>
        <span v-if="scope.row.status === 4" style="color: #f56c6c">驳回</span>
      </template>
      <template #refStatus="scope">
        <span v-if="scope.row.refStatus === 1">已退费</span>
        <span v-if="scope.row.refStatus === 0">未退费</span>
      </template>

      <template #operation="scope">
        <el-upload
          v-if="scope.row.status === 1 || scope.row.status === 4"
          v-hasPermi="['yuanshidian:ab01:ab01upload']"
          class="upload-demo"
          :action="uploadImgUrl"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          name="file"
          :show-file-list="false"
          :data="paramsDataObj"
          :on-success="handleSuccess"
          :on-error="handleError"
          :headers="headers"
          :limit="1"
          style="display: inline-block"
          :file-list="fileList"
        >
          <el-button link type="primary" @click="updateFile(scope.row)"
            >上传附件
            <span v-if="scope.row.fileurl" style="color: red">*</span>
          </el-button>
        </el-upload>
        <el-button link type="primary" @click="lookFile(scope.row)">查看附件</el-button>
        <el-button v-if="scope.row.status === 2" v-hasPermi="['yuanshidian:ab01:examine']" link type="primary" @click="handleAudit(scope.row)"
          >审核</el-button
        >

        <my-button link type="primary" title="人员信息导出" @click="handleRefund1(scope.row)" />
        <my-button link type="primary" title="单位缴费导出" @click="handleRefund(scope.row)" />
        <el-button v-if="scope.row.status === 3" link type="primary" @click="handleContrast(scope.row)">对比</el-button>
        <el-button link type="primary" @click="handleDetail(scope.row)">详情</el-button>
        <el-button
          v-if="scope.row.status === 1 || scope.row.status === 4"
          v-hasPermi="['yuanshidian:ab01:edit']"
          link
          type="primary"
          @click="handleUpdate(scope.row)"
          >修改</el-button
        >
        <el-button
          v-if="scope.row.status === 1 || scope.row.status === 4"
          v-hasPermi="['yuanshidian:ab01:submit']"
          link
          type="primary"
          @click="handleSumbit(scope.row)"
          >提交</el-button
        >
        <my-button v-if="scope.row.status === 3" link type="text" expand-type="danger" @click="handleReturn(scope.row)">驳回</my-button>
        <el-button
          v-if="scope.row.status === 1 || scope.row.status === 4"
          v-hasPermi="['yuanshidian:ab01:remove']"
          link
          type="danger"
          @click="handleDelete(scope.row)"
          >删除</el-button
        >
      </template>
    </my-table>
    <template #footer>
      <pagination v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </template>
  </InlineLayout>
  <addOrEdit v-model="dialog.visible" :dialog-title="dialog.title" :current-row="currentRow" @handle-query="handleQuery" />
  <contrast v-model="dialog.isShow" :current-row="currentRow" />
  <audit v-model="dialog.auditIsShow" :current-row="currentRow" @audit-sumbit="auditSumit" />
  <exportList v-model="dialog.isEXShow" :dialog-title="dialog.title" :current-row="currentRow" :is-per="isPer" />
</template>

<script setup name="Ab01" lang="ts">
import { listAb01, delAb01, updateStatus, examine, turndownab01 } from '@/api/yuanshidian/ab01';
import { addAb01new } from '@/api/yuanshidian/ab01new';
import { Ab01VO, Ab01Query, Ab01Form } from '@/api/yuanshidian/ab01/types';
import addOrEdit from './components/addOrEdit.vue';
import contrast from './components/contrast.vue';
import exportList from './components/exportList.vue';
import audit from '@/views/components/audit/index.vue';
import { globalHeaders } from '@/utils/request';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const ab01List = ref<Ab01VO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const isPer = ref(false);
const total = ref(0);
const currentRow = ref({});
const headers = ref(globalHeaders());
const fileList = ref([]);
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const uploadImgUrl = ref(baseUrl + '/yuanshidian/ab01/ab01upload'); // 上传的图片服务器地址
const paramsDataObj = ref({});
const queryFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  auditIsShow: false,
  isEXShow: false,
  title: '',
  isShow: false
});

const data = reactive<PageData<Ab01Form, Ab01Query>>({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dw01: '',
    refStatus: 0,
    delStatus: 0,
    dw02: ''
  }
});
const state = reactive({
  itemsDatas: [
    { label: '单位编码', prop: 'dw01', type: 'input' },
    { label: '单位名称', prop: 'dw02', type: 'input' },
    {
      label: '退费标识',
      prop: 'refStatus',
      type: 'select',
      options: [
        { value: 0, label: '未退费' },
        { value: 1, label: '已退费' }
      ]
    }
  ],
  tableData: [],
  columns: [
    { label: '序号', type: 'index', width: '55', prop: 'index' },
    { type: 'selection' },
    { label: '统筹区编码', prop: 'aaa027', align: 'center', width: '100' },
    { label: '单位编码', prop: 'dw01', align: 'center', width: '100' },
    { label: '单位名称', prop: 'dw02', align: 'center', minWidth: '80' },
    { label: '详细地址', prop: 'dw04', align: 'center', width: '180' },
    { label: '单位开户行', prop: 'dw08', align: 'center', width: '220' },
    { label: '单位法人', prop: 'dw10', align: 'center', width: '80' },
    { label: '单位联系人', prop: 'dw12', align: 'center', width: '100' },
    { label: '退费标识', prop: 'refStatus', align: 'center', type: 'custom', slot: 'refStatus', width: '100' },
    { label: '数据状态', prop: 'status', align: 'center', type: 'custom', slot: 'status', width: '80' },
    { label: '操作', type: 'operation', minWidth: '80px' }
  ]
});
const { itemsDatas, tableData, columns, dataForm } = toRefs(state);
const { queryParams } = toRefs(data);

/** 查询原单位信息列表 */
const getList = async () => {
  loading.value = true;
  tableData.value = [];
  const res = await listAb01(queryParams.value);
  tableData.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: Ab01VO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  dialog.visible = true;
  currentRow.value.id = '';
  currentRow.value.type = 'add';
  dialog.title = '添加原单位信息';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: Ab01VO) => {
  currentRow.value.id = row.id;
  currentRow.value.type = 'edit';
  dialog.title = '修改原单位信息';
  dialog.visible = true;
};
/** 查看详情 */
const handleDetail = (row) => {
  currentRow.value.id = row.id;
  currentRow.value.type = 'detail';
  dialog.title = '查看原单位信息';
  dialog.visible = true;
};
//对比
const handleContrast = (row) => {
  currentRow.value.id = row.id;
  dialog.isShow = true;
};
//查看附件
const lookFile = (row) => {
  if (!row.fileurl) {
    proxy?.$modal.msgError('没有可查看的附件');
    return;
  }
  window.open(row.fileurl, '_blank');
};
/** 删除按钮操作 */
const handleDelete = async (row?: Ab01VO) => {
  const _ids = row?.id;
  await proxy?.$modal.confirm('是否确认删除原单位信息编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delAb01(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

//提交
const handleSumbit = async (row) => {
  var res = await updateStatus(row.id);
  proxy?.$modal.msgSuccess('提交成功');
  await getList();
};
/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'yuanshidian/ab01/export',
    {
      ...queryParams.value,
      splitID: ids.value.length > 0 ? ids.value.join(',') : ''
    },
    `单位信息_${new Date().getTime()}.xlsx`
  );
};
//上传附件
const updateFile = (data) => {
  paramsDataObj.value.ID = data.id;
};
const handlePreview = () => {};
const handleRemove = () => {};
//附件大小限制
const beforeAvatarUpload = (file) => {
  const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'application/pdf';
  const isLt2M = file.size / 1024 / 1024 < 10;
  if (!isJPG) {
    proxy?.$modal.msgError('附件格式为jpg、png、pdf，请重新上传!');
  }
  if (!isLt2M) {
    proxy?.$modal.msgError('上传附件大小不能超过 10MB!');
  }
  return isLt2M && isJPG;
};
//附件上传成功回调
const handleSuccess = (data) => {
  if (data.code === 200) {
    proxy?.$modal.msgSuccess(data.msg);
    fileList.value = [];
    getList();
  } else {
    proxy?.$modal.msgError(data.msg);
  }
};
//附件上传失败回调
const handleError = (data) => {
  proxy?.$modal.msgSuccess(data.msg);
};
const handleAudit = (data) => {
  currentRow.value = data;
  dialog.auditIsShow = true;
};
//审核
const auditSumit = async (data) => {
  var params = {
    ...data,
    //id: currentRow.value.id,
    dwname: currentRow.value.dw02,
    dwcode: currentRow.value.dw01,
    examinedataType: 1
  };
  var res = await examine(params);
  if (res.code === 200) {
    proxy?.$modal.msgSuccess('审核成功');
    addAb01new(currentRow.value);
  }
  getList();
};
//驳回
const handleReturn = async (data) => {
  var res = await turndownab01(data.id);
  if (res.code === 200) {
    proxy?.$modal.msgSuccess('驳回成功');
  }
  getList();
};
/** 缴费办理操作 */
const handleRefund = (row) => {
  currentRow.value = row;
  isPer.value = false;
  dialog.title = '退费办理';
  dialog.isEXShow = true;
};
/** 缴费办理操作 */
const handleRefund1 = (row) => {
  currentRow.value = row;
  isPer.value = true;
  dialog.title = '人员信息导出';
  dialog.isEXShow = true;
};
onMounted(() => {
  getList();
});
</script>
